﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common.data.csv;
using net.veekee.common.data;
using net.veekee.common.rules;
using net.veekee.common;
using net.veekee.logprocessor.plugin.input;

namespace net.veekee.logprocessor.plugin.input.pack
{
    public class CSVProcessor : AProcessor<FileEntry>
    {
        protected override bool ProcessFile(String archive, String filename)
        {
            try
            {
                CSVReaderFile reader = new CSVReaderFile(filename, Encoding.GetEncoding(Options.GetInstance().Get("input.encoding")));

                reader.LoadHeader();
                
                bool stillOK = true;
                while (stillOK)
                {
                    this.stats_read++;
                    FileEntry entry = reader.Next<FileEntry>();
                    if (entry == null)
                        break;
                    if (entry.Fields.ItemCount == 0)
                    {
                        this.stats_discarded++;
                        this.OnNewDiscardedItem(entry);
                        break;
                    }

                    this.OnNewItem(entry);
                }
                return true;
            }
            catch (Exception e)
            {
                Log.GetInstance().Error("{0}", e.ToString());
            }
            finally
            {
                Log.GetInstance().WriteProgressBarEnd();
            }
            return false;
        }
    }
}
